package com.vanysoft.business.demo.table.controller;

import com.vanysoft.business.demo.table.entity.TDemoTable;
import com.vanysoft.business.demo.table.service.DemoTableService;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.business.system.dataDictionary.service.DataDictionaryService;
import com.vanysoft.common.utils.string.StringUtil;
import com.vanysoft.framework.aop.VanySoftValidation;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;

/**
 * 类名称：DemoTableAction
 * 类描述：DEMO列表控制层类
 * 创建人：SharpClaw_X
 * 创建时间：2017年10月22日上午11:57:31
 * 修改人：SharpClaw_X
 * 修改时间：2017年10月22日
 * 版本信息：@version 1.0
 * Copyright (c) 2017 SharpClaw
 * 修改备注：
 */
@RestController
@RequestMapping("/demo/demotable")
public class DemoTableAction extends BaseController {

    @Autowired
    private DemoTableService demoTableService;
    @Autowired
    private DataDictionaryService dataDictionaryService;

    /**
     * 获取
     * Function Name               loginValidate
     *
     * @return
     * @throws VanySoftBaseException
     * @description 查询类型数据
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年10月22日上午11:57:16          SharpClaw_X          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/queryTree", method = RequestMethod.GET)
    public JSONResponse queryTree(HttpServletRequest request) throws VanySoftBaseException {
        List<Map<String, Object>> list = dataDictionaryService.querySystemDataTable();
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, list);
    }

    /**
     * Function Name               queryOrgUserList
     *
     * @return
     * @throws VanySoftBaseException
     * @description 查询表单数据
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年10月22日下午12:45:42          SharpClaw_X          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/queryList", method = RequestMethod.GET)
    public JSONResponse queryOrgUserList(@SessionUser UserInfo userInfo, HttpServletRequest request, String scrollPageSize, String scrollPageIndex, String dataType, String searchValue) throws NumberFormatException, VanySoftBaseException {
        Integer _pageIndex = StringUtil.isEmpty(scrollPageIndex) ? 1 : Integer.valueOf(scrollPageIndex);
        Integer _pageSize = StringUtil.isEmpty(scrollPageSize) ? 10 : Integer.valueOf(scrollPageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        if (userInfo != null) {
            map = demoTableService.queryDemoTable(_pageSize, _pageIndex, dataType, searchValue);
        }
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, map);
    }


    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @VanySoftValidation
    public JSONResponse addDemoTable(@SessionUser UserInfo userInfo, HttpServletRequest request, @Valid TDemoTable demoTable) throws VanySoftBaseException {
        String demoTableKey = UUID.randomUUID().toString();
        //String demoTableKey = "02b2a57b717847dda69d07055e4e1c63";
        demoTable.setOprDate(new Date());
        demoTable.setDemoTableKey(demoTableKey);
        demoTable.setUserAccount(userInfo.getUsername());
        demoTableService.addDemoTable(demoTable);
        return new JSONResponse();
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    @VanySoftValidation
    public JSONResponse updateDemoTable(@SessionUser UserInfo userInfo, HttpServletRequest request, @Valid TDemoTable demoTable) throws VanySoftBaseException {
        demoTable.setOprDate(new Date());
        demoTable.setUserAccount(userInfo.getUsername());
        demoTableService.updateDemoTable(demoTable);
        return new JSONResponse();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @VanySoftValidation
    public JSONResponse deleteDemoTable(@SessionUser UserInfo userInfo, HttpServletRequest request, @Valid String demoTableKey) throws VanySoftBaseException {
        demoTableService.deleteDemoTable(demoTableKey);
        return new JSONResponse();
    }

    @RequestMapping(value = "/querycols", method = RequestMethod.GET)
    @VanySoftValidation
    public JSONResponse querycols(HttpServletRequest request, String tablename) throws VanySoftBaseException {

        List<Map<String, Object>> list = this.demoTableService.getTableCols(tablename);
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, list);
    }
}
